<?php
    require_once "../../config.php";
    require_once "../../functions.php";
    //获取前端传来的参数
    $currentPage = $_POST["currentPage"];//当前第几页
    $pageSize = $_POST["pageSize"];//一页有多少条数据
    $offset = ($currentPage - 1) * $pageSize;//从数据的索引几开始
    //获取文章状态
    $status = $_POST["status"];
    //获取文章的分类
    $categoryId = $_POST["categoryId"];
    //声明一个变量,用来判断拼接sql语句的where条件
    $where = " where 1 = 1";
    
    //判断如果status是all时,就表示查询所有的数据,这时就不需要where条件了
    if($status != "all"){
        $where.=" and p.status = '{$status}'";
    }
    //判断如果分类的id是all时,就表示查询所有数据,这时也就不需要where条件了
    if($categoryId != "all"){
        $where.= " and p.category_id = {$categoryId}";
    }
    //连接数据库
    $connect = connect();
    //sql
    $querySql = "SELECT p.id, p.title, p.created,p.`status`,u.nickname,c.`name`
    from posts p
    left JOIN users u ON u.id = p.user_id
    LEFT JOIN categories c ON  c.id=p.category_id
    {$where}
    LIMIT {$offset},{$pageSize}";

    //执行sql返回二维数组
    $queryResult = querySelect($connect,$querySql);
    // 返回前端的信息提示
    $response = ["code"=>0,"msg"=>"操作失败"];
    //判断查询是否成功
    if($queryResult){
        $response["code"] = 1;
        $response["msg"] = "操作成功";
        $response["data"] = $queryResult;
    }
    header("content-type:application/json;charset=ut8");
    echo json_encode($response);
?>